package demo

import (
	"fmt"
	"server/utils"

	"github.com/gin-gonic/gin"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
	tts "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tts/v20190823"
)

// 腾讯云基础语音合成文档：https://cloud.tencent.com/document/product/1073/37995#.E7.A4.BA.E4.BE.8B1-API.E8.B0.83.E7.94.A8

func PostTextToVoice(c *gin.Context) {
	// 取参
	var postData struct {
		Text      string `json:"text"`
		VoiceType int64  `json:"voice_type"`
	}
	c.BindJSON(&postData)

	credential := common.NewCredential(
		"AKIDLUN0N16aVAYgY9LXLtABw5ze5HEvwhhQ ",
		"bw1FF0Wnpb8QTIsDJAm9HAoDNIyPbYFg",
	)
	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = "tts.tencentcloudapi.com"
	client, _ := tts.NewClient(credential, "ap-guangzhou", cpf)

	request := tts.NewTextToVoiceRequest()
	fmt.Printf("%#v\n", postData)
	request.Text = common.StringPtr(postData.Text)
	request.VoiceType = common.Int64Ptr(postData.VoiceType)
	request.SessionId = common.StringPtr("AKIDLUN0N16aVAYgY9LXLtABw5ze5HEvwhhQ")
	request.ModelType = common.Int64Ptr(1)

	response, err := client.TextToVoice(request)
	if _, ok := err.(*errors.TencentCloudSDKError); ok {
		c.AbortWithError(500, err)
		return
	}
	if err != nil {
		c.AbortWithError(500, err)
		return
	}
	// 正常返回数据
	c.JSON(
		200,
		utils.DealResponse(utils.Response{
			"data": response.Response.Audio,
		}),
	)
}
